package com.viewpagertext.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.viewpagertext.R;
import com.viewpagertext.constructor.Song;
import com.viewpagertext.help.MediaPlayerHelp;
import com.viewpagertext.views.MyRoundedImageView;

import org.w3c.dom.Text;

import java.util.List;
import java.util.Map;

public class LocalFoldExpandableListviewAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> groupItems;
    private Map<String, List<Song>> childItems;
    private boolean isPlaying;
    private MediaPlayer mediaPlayer;
    private String csong, csinger, calbum, cduration, cpath;
    private long csize;
    private Bitmap cbitmap;
    private MediaPlayerHelp mMediaPlayerHelp;
    private BottomSheetDialog bottomSheetDialog;
    private MyRoundedImageView localDialogIcon;
    private String type;

    public LocalFoldExpandableListviewAdapter(Context context, List<String> groupItems, Map<String, List<Song>> childItems, String type) {
        this.context = context;
        this.groupItems = groupItems;
        this.childItems = childItems;
        this.type = type;
    }

    @Override
    public int getGroupCount() {
        return groupItems.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childItems.get(groupItems.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupItems.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childItems.get(groupItems.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.local_group_layout, null);
        }
        TextView textView = convertView.findViewById(R.id.fold_name);
        textView.setText(getGroup(groupPosition).toString());

        ImageView imageView = convertView.findViewById(R.id.localmenu);
//        imageView.setImageResource(R.mipmap.album);
        switch (type) {
            case "singer":
                imageView.setImageResource(R.mipmap.singer);
                break;
            case "album":
                imageView.setImageResource(R.mipmap.album);
                break;
            default:
                imageView.setImageResource(R.mipmap.fold);
                break;
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_layout, null);
        }
        TextView song = convertView.findViewById(R.id.Listt_song);
        song.setText(((Song) getChild(groupPosition, childPosition)).song);
        TextView singer = convertView.findViewById(R.id.Listt_singer);
        singer.setText(((Song) getChild(groupPosition, childPosition)).singer);
        TextView positon = convertView.findViewById(R.id.Listt_postion);
        positon.setText(String.valueOf(childPosition + 1));
        ImageView imageView = convertView.findViewById(R.id.ListFragment_menu);
        imageView.setVisibility(View.INVISIBLE);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
